// 设置
function setItem(name, item) {
    localStorage.setItem(name, JSON.stringify(item))
}
// 获取
function getItem(name) {
    return JSON.parse(localStorage.getItem(name) || "[]")
}
// 删除
function removeItem(name) {
    return localStorage.removeItem(name)
}

function sortByKey(array, key) {
    return array.sort(function (a, b) {
        var x = a[key]
        var y = b[key]
        return x < y ? -1 : (x > y ? 1 : 0)

    })
}
const actions = {}

const mutations = {
    // 添加商品到购物车
    addCar(state, goodsInfo) {
        console.log("goodsInfo:", goodsInfo);
        var flag = false;
        state.car.some((item) => {
            // 判断商品的skuid是否已经存在购物车中 
            if (item.skuId === goodsInfo.skuId) {
                item.buyAmount += parseInt(goodsInfo.buyAmount)
                flag = true
                return true;
            }
        })
        if (!flag) {
            state.car.push(goodsInfo)
            sortByKey(state.car, 'id')
        }
        setItem('car', state.car)
    },
    // 改变商品的选择状态
    updateGoodsSelected(state, info) {
        state.car.some((item) => {
            if (item.skuId === info.id) {
                item.selected = info.selected
            }
        })
        setItem('car', state.car)
    },
    // 改变所以商品的状态为treu 或 false
    updatapd(state, pd) {
        state.car.some((item) => {
            item.selected = pd
        })
        setItem('car', state.car)
    },
    // 清空购物车
    clearCar(state) {
        console.log(state);
        state.car = []
        removeItem('car')
    },
    // 删除商品
    removeCar(state, id) {
        state.car.some((item, i) => {
            if (item.skuId == id) {
                state.car.splice(i, 1)
                return true
            }
        })
        setItem('car', state.car)
    },
    // 商品数量的改变调用
    updateGoodsInfo(state, info) {
        state.car.some((item) => {
            if (item.skuId === info.id) {
                // console.log(item);
                item.buyAmount = info.buyAmount
            }
        })
        setItem('car', state.car)
    }
}

const state = {
    car: getItem('car')
}
const getters = {
    // 获取商品数量
    getGoodsCount(state) {
        var goods = {}
        state.car.forEach((item) => {
            goods[item.skuId] = item.buyAmount
        })
        return goods;
    },
    // 获取商品有没有选中的状态值
    getGoodsSelected(state) {
        var goods = {}
        state.car.forEach((item) => {
            goods[item.skuId] = item.selected
        })
        return goods;
    },
    // 获取商品个数
    getCount(state) {
        return state.car.length || ''
    },
    // 获取选中的商品的件数
    getSelectedCount(state) {
        var count = 0
        state.car.forEach((item) => {
            if (item.selected) {
                count += 1;
            }
        })
        return count;
    },
    // 获取选中的商品
    getSelectedGoods(state) {
        var goods = {}
        state.car.forEach((item) => {
            if (item.selected) {
                goods[item.skuId] = item
            }
        })
        return goods;
    }
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}